/**  
 * All rights Reserved, Designed By www.beonelot.com
 * @Package: com.bccah.modlib.dbdo.controller
 * @Filename: AcsControlBlockInfoController.java
 * @Description: TODO(AcsControlBlockInfoController)
 * @author: 金鑫智慧
 * @date: 2020-04-03
 * @version: V1.0
 * @Copyright: 2018 www.beonelot.com Inc. All rights reserved.
 * 注意：本内容仅限于重庆金鑫智慧科技有限公司内部传阅，禁止外泄以及用于其他的商业目的
 */
package com.jinxin.platform.cdfaceacs.controller;

import com.github.pagehelper.PageInfo;
import com.jinxin.platform.base.common.pojo.InvokeCfg;
import com.jinxin.platform.base.common.pojo.JsonResult;
import com.jinxin.platform.base.common.support.BusinessDelegate;
import com.jinxin.platform.cdfaceacs.common.CommonCodeEntity;
import com.jinxin.platform.cdfaceacs.common.ResultEntity;
import com.jinxin.platform.cdfaceacs.pojo.domains.AcsControlSysParamDO;
import com.jinxin.platform.cdfaceacs.pojo.vo.AcsControlSysParamVO;
import com.jinxin.platform.cdfaceacs.service.AcsControlSysParamService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * TODO(AcsControlSysParamController)
 * @Classn: AcsControlSysParamController
 * @author SY
 * @date 2020-04-03
 */
@RestController
@RequestMapping("/basicSysParam")
@Slf4j
public class AcsControlSysParamController {

    @Autowired
    private AcsControlSysParamService sysParamService;
    @Autowired
    private BusinessDelegate businessDelegate;

    /**
     * 获取系统信息的分页表
     * @param page
     * @param limit
     * @return
     */
    @PostMapping("/paramTable")
    public Map getParamTable( int page, int limit  ){
        Map<String,Object> resultMap = new HashMap<>();
        PageInfo<AcsControlSysParamDO> pageInfo = sysParamService.selParamPage(page,limit );
        resultMap.put("code","0");
        resultMap.put("msg","");
        resultMap.put("count",pageInfo.getTotal());
        resultMap.put("data",pageInfo.getList());
        return resultMap;
    }

    /**
     * 添加系统参数
     * @param paramVO
     * @return
     */
    @PostMapping("/addParamInfo")
    public ResultEntity addParamInfo(@Valid AcsControlSysParamVO paramVO){
//        Map<String,Object> resMap = checkKey(paramVO);
//        if((Boolean)resMap.get("res")){
            AcsControlSysParamDO infoDO = new AcsControlSysParamDO();
            BeanUtils.copyProperties(paramVO,infoDO );
            infoDO.setCreateTime(LocalDateTime.now());
            sysParamService.saveParam(infoDO);
            log.info(infoDO.toString());
            return new ResultEntity(CommonCodeEntity.SUC_CODE,"suc","");
//        }else{
//            log.info(paramVO.getParamKey()+"已存在");
//            return new ResultEntity(CommonCodeEntity.ALREADY_EXIST,paramVO.getParamKey()+"已存在","");
//        }

    }

    /**
     * 编辑系统参数
     * @param paramVO
     * @return
     */
    @PostMapping("/upParamInfo")
    public ResultEntity upParamInfo(@Valid AcsControlSysParamVO paramVO){
        if(StringUtils.isEmpty(paramVO.getAcsSysParamId() )){
            return new ResultEntity(CommonCodeEntity.PARAM_ERR,"ID为空","" );
        }
//        Map<String,Object> resMap = checkKey(paramVO);
//        if(!(Boolean)resMap.get("res")){
//            List<AcsControlSysParamDO> paramDOList = (List<AcsControlSysParamDO>) resMap.get("exitID");
//            if(paramDOList.size()>1|| !paramDOList.get(0).getAcsSysParamId().equals(paramVO.getAcsSysParamId() ) ){
//                log.info(paramVO.getParamKey()+"已存在");
//                return new ResultEntity(CommonCodeEntity.ALREADY_EXIST,paramVO.getParamKey()+"已存在","");
//            }
//        }
        AcsControlSysParamDO infoDO = new AcsControlSysParamDO();
        BeanUtils.copyProperties(paramVO,infoDO );
        infoDO.setCreateTime(LocalDateTime.now());
        sysParamService.updateParam(infoDO);
        log.info(infoDO.toString());
        return new ResultEntity(CommonCodeEntity.SUC_CODE,"suc","");

    }

    /**
     * 删除系统参数
     * @param pid
     * @return
     */
    @PostMapping("/delParamInfo")
    public ResultEntity delParamInfo(String pid){
        if(StringUtils.isEmpty(pid)){
            return new ResultEntity(CommonCodeEntity.PARAM_ERR,"ID为空","" );
        }
        sysParamService.delParam(pid);
        log.info("删除记录ID--"+pid);
        return new ResultEntity(CommonCodeEntity.SUC_CODE,"suc","");
    }

//    Map<String,Object> checkKey(AcsControlSysParamVO paramVO){
//        Map<String,Object > resMap = new HashMap<>();
//        AcsControlSysParamDO infoDO = new AcsControlSysParamDO();
//        infoDO.setParamKey(paramVO.getParamKey());
//        List<AcsControlSysParamDO> paramDOList = sysParamService.selParamByInfo(infoDO);
//        if(paramDOList != null && paramDOList.size()>0){
//            log.info(paramVO.getParamKey()+"已存在");
//            resMap.put("res",false);
//            resMap.put("exitID",paramDOList);
//            resMap.put("msg", paramVO.getParamKey()+"已存在");
//            return resMap;
//        }
//        resMap.put("res",true);
//        return resMap;
//    }

    @GetMapping({"/test"})
    public String test() {
        InvokeCfg cfg = new InvokeCfg();
        cfg.setModuleKey("dev");
        cfg.setClassName("DevRpcForOther");
        cfg.setFunName("getBccaAccountWithCondition");
        cfg.setArgs(new HashMap());
        JsonResult jsonResult = this.businessDelegate.invoke(cfg);
        log.info(jsonResult.toString());
        return null;
    }

}
